package com.springboot.dense.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.springboot.dense.dto.FogLevelDTO;
import com.springboot.dense.entity.FogLevel;
import com.springboot.dense.entity.MonitoringPoint;
import com.springboot.dense.service.FogLevelService;
import com.springboot.dense.service.MonitoringPointService;
import com.springboot.dense.vo.CityDirectionVO;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-04-14
 */
@RestController
public class MonitoringPointCtrl {

    @Autowired
    private MonitoringPointService monitoringPointService;

    @GetMapping("/admin/CityAll")
    public JSONObject MonitoringPointAll() {
        List<MonitoringPoint> MonitoringPointList= monitoringPointService.list(null);
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "获取成功！");
        jsonObject.put("City", MonitoringPointList);
        jsonObject.put("top", "top_city");
        jsonObject.put("show_left", "left_city");
        jsonObject.put("left", "left_y_city");
        return jsonObject;
    }

    @PostMapping("/admin/CitySearch")
    public JSONObject MonitoringPointSearch(String city, String province) {
        QueryWrapper<MonitoringPoint> queryWrapper = new QueryWrapper<>();
        if(!city.equals("no") && province.equals("no")){
            queryWrapper.eq("city",city);
        }else if(city.equals("no") && !province.equals("no")){
            queryWrapper.eq("province",province);
        }else{
            queryWrapper.eq("city",city);
            queryWrapper.eq("province",province);
        }
        List<MonitoringPoint> MonitoringPointList = monitoringPointService.list(queryWrapper);
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "获取成功！");
        jsonObject.put("City", MonitoringPointList);
        return jsonObject;
    }

    @PostMapping("/admin/CityDelete")
    public JSONObject MonitoringPointDelete(Integer id) {
        monitoringPointService.removeById(id);
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "删除成功！");
        return jsonObject;
    }

    @GetMapping("/admin/CityToAdd")
    public JSONObject MonitoringPointAdd() {
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "初始化");
        jsonObject.put("top", "top_city");
        jsonObject.put("show_left", "left_addcity");
        jsonObject.put("left", "left_y_city");
        return jsonObject;
    }

    @PostMapping("/admin/CityOne")
    public JSONObject MonitoringPointOne(Integer id) {
        MonitoringPoint CityList= monitoringPointService.getById(id);
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "查询成功！");
        jsonObject.put("City", CityList);
        jsonObject.put("top", "top_city");
        jsonObject.put("show_left", "left_addcity");
        jsonObject.put("left", "left_y_city");
        return jsonObject;
    }

    @PostMapping("/admin/CityAddOrUpdate")
    public JSONObject MonitoringPointAddOrUpdate(MonitoringPoint monitoringPoint) {
//        UpdateWrapper<MonitoringPoint> updateWrapper = new UpdateWrapper<MonitoringPoint>();
//        updateWrapper.eq("city",monitoringPoint.getCity());
//        updateWrapper.eq("province",monitoringPoint.getProvince());
        monitoringPointService.saveOrUpdate(monitoringPoint);
        JSONObject jsonObject = new JSONObject();
        //code==0 表示成功
        jsonObject.put("code", 0);
        jsonObject.put("message", "修改成功！");
        return jsonObject;
    }

/******************************************* 前（下） 后（上）台 分割线 ***************************************/

    @GetMapping("/font/cityDetailList")
    public JSONObject cityDetailList(){
        JSONObject jsonObject = new JSONObject();
        List<MonitoringPoint> monitoringPointList = monitoringPointService.list(null);
        monitoringPointList.forEach(monitoringPoint -> {
            CityDirectionVO cityDirectionVO = new CityDirectionVO();
            cityDirectionVO.setCity(monitoringPoint.getCity());
            cityDirectionVO.getDirectList().add(monitoringPoint.getLongitude());
            cityDirectionVO.getDirectList().add(monitoringPoint.getLatitude());
            jsonObject.put(cityDirectionVO.getCity(),cityDirectionVO.getDirectList());
        });
        return jsonObject;
    }

}

